package com.qu.waibao.my.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TableRow;

import com.example.xlhratingbar_lib.XLHRatingBar;
import com.google.gson.reflect.TypeToken;
import com.othershe.nicedialog.BaseNiceDialog;
import com.othershe.nicedialog.NiceDialog;
import com.othershe.nicedialog.ViewConvertListener;
import com.othershe.nicedialog.ViewHolder;
import com.qu.waibao.AppConfig;
import com.qu.waibao.R;
import com.qu.waibao.base.BaseFragment;
import com.qu.waibao.base.BaseRecyclerAdapter;
import com.qu.waibao.bean.Order;
import com.qu.waibao.home.activity.PublishDetailActivity;
import com.qu.waibao.interf.XnetCallBack;
import com.qu.waibao.my.adapter.AcceptAdapter;
import com.qu.waibao.network.ActionUtil;
import com.qu.waibao.network.NetManager;
import com.qu.waibao.utils.GsonUtil;
import com.qu.waibao.utils.LogUtil;
import com.qu.waibao.utils.ToastUtil;
import com.qu.waibao.widget.RecyclerRefreshLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/7/23.
 */

public class AcceptOrderFragemnt extends BaseFragment implements
        RecyclerRefreshLayout.SuperRefreshLayoutListener,
        BaseRecyclerAdapter.OnItemClickListener,AcceptAdapter.OnCommentItemClickListener{
    @BindView(R.id.mRecyclerRefreshLayout)
    RecyclerRefreshLayout mRecyclerRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private AcceptAdapter adapter;
    private boolean isRefreshing = true;
    private int pageNum = 1;
    private String type = "";

    @Override
    protected void initBundle(Bundle bundle) {
        super.initBundle(bundle);
        if(bundle != null){
            type = bundle.getString("type","");
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_accept;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        mRecyclerRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
        mRecyclerRefreshLayout.setSuperRefreshLayoutListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new AcceptAdapter(mContext, BaseRecyclerAdapter.ONLY_FOOTER);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
        if(adapter.getItems().size() == 0){
            mRecyclerRefreshLayout.setRefreshing(true);
            onRefreshing();
        }
        adapter.setOnItemClickListener(this);
        adapter.setOnItemButtonClickListener(this);
    }

    public void requestData(){
        Map<String,String> map = new HashMap<>();
        map.put("action", ActionUtil.order_list_get);
        map.put("userid", AppConfig.getString("userid"));
        map.put("ordertype","2");
        map.put("state",type);
        map.put("keyword","");
        map.put("pagenum",pageNum+"");
        NetManager.getInstance().request(getActivity(), map, true, new XnetCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                List<Order> list = GsonUtil.createGson().fromJson(result,
                        new TypeToken<List<Order>>(){}.getType());
                if(isRefreshing){
                    adapter.clear();
                    adapter.addAll(list);
                    if(list.size() < 12){
                        adapter.setState(BaseRecyclerAdapter.STATE_NO_MORE,true);
                    }
                }else {
                    adapter.addAll(list);
                    if(list.size() < 12){
                        adapter.setState(BaseRecyclerAdapter.STATE_NO_MORE,true);
                    }
                    if(list.size() == 0) pageNum--;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if(pageNum >1){
                    pageNum--;
                }
                onComplate();
            }

            @Override
            public void onFinished() {
                onComplate();
            }
        });
    }

    public void onComplate(){
        mRecyclerRefreshLayout.onComplete();
        isRefreshing = false;
    }

    @Override
    public void onRefreshing() {
        isRefreshing = true;
        pageNum = 1;
        requestData();
    }

    @Override
    public void onLoadMore() {
        isRefreshing = false;
        pageNum++;
        adapter.setState(BaseRecyclerAdapter.STATE_LOAD_MORE, true);
        requestData();
    }

    @Override
    public void onCommentItemClick(int position) {
        final String serviceid = adapter.getItems().get(position).getServiceid();
        NiceDialog.init()
                .setLayoutId(R.layout.comment_dialog_layout)     //设置dialog布局文件
                .setConvertListener(new ViewConvertListener() {     //进行相关View操作的回调
                    @Override
                    public void convertView(final ViewHolder holder, final BaseNiceDialog dialog) {
                        holder.setOnClickListener(R.id.tv_submit, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                XLHRatingBar bar = holder.getView(R.id.ratingBar);
                                EditText remark = holder.getView(R.id.et_remark);
                                String score = bar.getCountSelected()+"";
                                String comment = remark.getText().toString();
                                LogUtil.Loge("TAG",comment+"::"+score);
                                if(TextUtils.isEmpty(comment)){
                                    ToastUtil.show(mContext,"评论不能为空");
                                    return;
                                }
                                comment(serviceid,comment,score);
                                dialog.dismiss();
                            }
                        });

                        holder.setOnClickListener(R.id.iv_delete, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });

                    }
                })
                .setMargin(30)
                .setDimAmount(0.3f)     //调节灰色背景透明度[0-1]，默认0.5f
                .setShowBottom(false)     //是否在底部显示dialog，默认flase
                .setOutCancel(true)     //点击dialog外是否可取消，默认true
                .show(getChildFragmentManager());     //显示dialog
    }

    @Override
    public void onItemClick(int position, long itemId) {
        String serviceid = adapter.getItems().get(position).getServiceid();
        Intent intent = new Intent(mContext, PublishDetailActivity.class);
        intent.putExtra("serviceid",serviceid);
        startActivity(intent);
    }

    public void comment(String serviceid,String remark,String score){
        Map<String,String> map = new HashMap<>();
        map.put("action",ActionUtil.order_comments_set);
        map.put("userid",AppConfig.getString("userid"));
        map.put("serviceid",serviceid);
        map.put("remark",remark);
        map.put("score",score);
        NetManager.getInstance().request(getActivity(), map, true, new XnetCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                ToastUtil.show(mContext,msg);
                onRefreshing();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
